Youtube 頻道:https://www.youtube.com/c/kaochenlong
如果畫面太小或看不清楚
可移駕至 https://www.youtube.com/watch?v=N2nmdRQWPlw 觀看 4K 高畫質版本
GitHub 專案:https://github.com/kaochenlong/cann-lang
程式碼:
檔名:src/helpers.ts
const isNumber = (char: string): boolean => /\d/.test(char)
export { isNumber }
檔名:tests/tokenizer.test.js
import { describe, it } from "https://deno.land/std@0.156.0/testing/bdd.ts"
import { expect } from "https://deno.land/x/expect@v0.2.10/mod.ts"
import { Tokenizer } from "../src/tokenizer.ts"
describe("Tokenizer", () => {
it("處理數字", () => {
const input = `9527`
const result = {
type: "NUMBER",
value: 9527,
}
const t = new Tokenizer(input)
expect(t.nextToken()).toEqual(result)
})
})
檔名:src/tokenizer.ts
import { isNumber } from "./helpers.ts"
type Token = {
type: string
value: any
}
class Tokenizer {
private input: string
private cursor: number
constructor(input: string) {
this.input = input
this.cursor = 0
}
nextToken(): Token | null {
if (this.input.length === this.cursor) {
return null
}
const str = this.input.slice(this.cursor)
// 如果是數字的話...
if (isNumber(str[0])) {
let result = ""
while (isNumber(str[this.cursor])) {
result += str[this.cursor]
this.cursor++
}
return {
type: "NUMBER",
value: +result,
}
}
return null
}
}
export { Tokenizer }
如果喜歡這個系列的影片,歡迎訂閱我的頻道
或是想聽我介紹一些別的內容,也可直接在這裡或 YouTube 頁面下方留言 :)